//
//  MainTabVC.swift
//  coredataTest
//
//  Created by xuNick on 16/11/10.
//  Copyright © 2016年 NN. All rights reserved.
//

import UIKit
import Alamofire
import CoreData

class MainTabVC: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.downloadData()
    }
    
    func downloadData(){
        //下载网咯数据
        Alamofire.request("http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_month.geojson").responseJSON { (response) in
            //            var dicts: [[String : String]]? = [[:]]
            
            let dicts = response.result.value as! [String: AnyObject]
            //装载数据库
            if let dicts = dicts["features"] as? [[String: AnyObject]] {
                for dict in dicts {
                    let quake = NSEntityDescription.insertNewObject(forEntityName: "Quake", into: CoredataManager.managedObjectContext) as! Quake
                    quake.updateFromDictionary(dict)
                }
                do {
                    try CoredataManager.managedObjectContext.save()
                } catch {
                    fatalError("Failure to save context: \(error)")
                }
            }
            
        }
    }
}
